package com.ponhu.ea2.goodsCenter.provider.facade.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ponhu.ea2.common.basic.model.PagedResult;
import com.ponhu.ea2.constants.CommonConstant;
import com.ponhu.ea2.dto.OperationRecordContentDto;
import com.ponhu.ea2.entity.OperationRecordLogs;
import com.ponhu.ea2.goodsCenter.provider.service.OperationRecordLogsService;
import com.ponhu.ea2.goodsCenter.provider.utils.DataConverterUtils;
import com.ponhu.ea2.query.OperationRecordLogsQuery;
import com.ponhu.ea2.service.facade.InventoryApiService;
import com.ponhu.ea2.service.facade.OperationRecordLogsApiService;
import com.ponhu.ea2.vo.OperationRecordLogsVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 操作记录日志API服务
 *
 * @author Wang
 * @date 2021/10/22 7:03 下午
 */
@DubboService(version = "1.0.0", interfaceClass = OperationRecordLogsApiService.class, timeout = 6000)
public class OperationRecordLogsApiServiceImpl implements OperationRecordLogsApiService {

    @Autowired
    private OperationRecordLogsService operationRecordLogsService;

    /**
     * 分页查询操作记录日志列表
     * @param operationRecordLogsQuery

     * @return
     */
    @Override
    public PagedResult<OperationRecordLogsVO> getOperationRecordLogsPage(OperationRecordLogsQuery operationRecordLogsQuery) {
        IPage<OperationRecordLogs> recordLogsPage = operationRecordLogsService.getOperationRecordLogsPage(operationRecordLogsQuery);
        List<OperationRecordLogs> records = recordLogsPage.getRecords();

        List<OperationRecordLogsVO> collect = records.stream().map(r -> {
            OperationRecordLogsVO operationRecordLogsVO = new OperationRecordLogsVO();
            BeanUtils.copyProperties(r, operationRecordLogsVO);

            if (r.getLogType() == CommonConstant.OPERATION_RECORD_LOG_TYPE.TABLE_COLUMNS_CHANGE && StringUtils.isNotBlank(r.getContent())) {
                try {
                    operationRecordLogsVO.setContentList(JSONArray.parseArray(r.getContent(), OperationRecordContentDto.class));
                } catch (Exception e) {
                    //nothing to do
                }
            }
            return operationRecordLogsVO;
        }).collect(Collectors.toList());

        return DataConverterUtils.convertToPagedResult(recordLogsPage, collect);
    }
}
